Add Liquid regex replace filters

Copied and tweaked the general idea from [here](https://github.com/Shopify/liquid/issues/202#issuecomment-19112872). Essentially using the same code that [Liquid uses](https://github.com/Shopify/liquid/blob/master/lib/liquid/standardfilters.rb#L185) for their `replace` filter.

HyShai 9 年之前
父節點
當前提交
91e97957e2
共有 1 個文件被更改,包括 9 次插入1 次删除
  1. 9 1
      app/concerns/liquid_interpolatable.rb

+ 9 - 1
app/concerns/liquid_interpolatable.rb

@@ -190,7 +190,15 @@ module LiquidInterpolatable
190 190
         'concat(' << subs.join(', ') << ')'
191 191
       end
192 192
     end
193
-
193
+    
194
+    def regex_replace(input, regex, replacement = ''.freeze)
195
+      input.to_s.gsub(Regexp.new(regex), replacement.to_s)
196
+    end
197
+    
198
+    def regex_replace_first(input, regex, replacement = ''.freeze)
199
+      input.to_s.sub(Regexp.new(regex), replacement.to_s)
200
+    end
201
+    
194 202
     private
195 203
 
196 204
     def logger